import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/*
 * @class InputCustom
 * @author: ZhongWb
 * @date: 2019/12/20 14:43
 * @description 输入框 - 默认样式为 搜索框样式
 **/
class InputCustom extends StatefulWidget {
  bool isPassWord;//是否密码
  GestureTapCallback onTap;//点击事件
  bool isShowClear ;//是否显示右侧图标
  String leftIcon;//左侧图标
  Color leftIconColor;//左侧图标颜色
  IconData rightIcon;//右侧图标
  Color rightIconColor;//右侧图标颜色
  String hinText;//提示文本
  ValueChanged onChanged;//输入改变时
  ValueChanged onSubmit;//回车提交时
  ValueChanged onRight;//右侧图标点击事件
  double height;//高度
  double widght;//高度
  double circular;//圆角
  double vertical;//文本居中调节
  double fontSzie;//文本大小
  double leftIconSize;//左侧图标大小
  double rightIconSize;//右侧图标大小
  Color bgColor;//背景色
  Color fontColor;//字体色
  Color hinColor;//字体色
  Color cursorColor;//光标颜色
  double cursorWidth;//光标大小
  TextEditingController controller;//控制器
  FocusNode focusNode;//焦点
  TextInputType keyboardType;//键盘类型
  bool isLeftIcon;
  InputCustom({
    this.onTap,
    this.onRight,
    this.isLeftIcon:true,
    this.hinColor:Colors.grey,
    this.isShowClear:true,
    this.onChanged,
    this.hinText : "输入搜索的内容",
    this.leftIcon,
    this.rightIcon,
    this.isPassWord:false,
    this.onSubmit,
    this.height:29,
    this.circular:20,
    this.vertical:9.2,
    this.bgColor:Colors.white,
    this.fontSzie:14,
    this.fontColor:Colors.black54,
    this.leftIconColor:Colors.grey,
    this.rightIconColor,
    this.cursorColor:Colors.grey,
    this.controller,
    this.focusNode,
    this.cursorWidth:1.5,
    this.leftIconSize:20,
    this.rightIconSize:18,
    this.keyboardType:TextInputType.text,
    this.widght:120,
  });
  @override
  _InputCustomState createState() => _InputCustomState();
}

class _InputCustomState extends State<InputCustom> {

  TextEditingController _connection = new TextEditingController();
  FocusNode _focusNode = new FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if(widget.controller!=null){
      setState(() {
        _connection = widget.controller;
      });
    }
    if(widget.focusNode!=null){
      setState(() {
        _focusNode = widget.focusNode;
      });
    }

  }
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          width: widget.widght,
          height: widget.height,
          decoration: BoxDecoration(
            color: widget.bgColor,
            borderRadius: BorderRadius.circular(widget.circular),
          ),
          padding: EdgeInsets.only(left: widget.isLeftIcon?30 :10),
          child: TextField(
            onTap: widget.onTap ?? (){},
            controller: _connection,
            obscureText: widget.isPassWord,//是否为密码
            cursorColor:widget.cursorColor,//光标颜色
            cursorWidth: widget.cursorWidth,
            onChanged: widget.onChanged,
            onSubmitted: widget.onSubmit,
            focusNode: _focusNode,
            keyboardType: widget.keyboardType,
            style: TextStyle(
              color: widget.fontColor,
              fontSize: widget.fontSzie,
            ),
            decoration: InputDecoration(
              //尾部添加清除按钮
              suffixIcon:widget.isShowClear && _connection !=null && _connection.text !=""
                  ? InkWell(
                child: Icon(widget.rightIcon ?? Icons.cancel,color: widget.rightIconColor ?? Color(0xffDBDBDB),size: widget.rightIconSize,),
                onTap: (){
                  // 保证在组件build的第一帧时才去触发取消清空内容
                  WidgetsBinding.instance.addPostFrameCallback((_) {
                    setState(() {
                      _connection.clear();
                    });
                    widget.onRight(_connection);
                  } );

                },
              ): null ,
              //调整内容是否居中 、负数往下推，正数往上推
              contentPadding:  EdgeInsets.symmetric(vertical: widget.vertical),
              hintText: widget.hinText,
              hintStyle: TextStyle(fontSize: widget.fontSzie,color: widget.hinColor),
             border: InputBorder.none,
            ),
          ),
        ),
        widget.isLeftIcon ? Positioned(top: 8.2,left: 10,child:Image.asset(widget.leftIcon ?? "images/package/search.png",color: widget.leftIconColor,width:widget.leftIconSize,height: widget.leftIconSize,fit: BoxFit.cover,),):Positioned(child: Container(height: widget.leftIconSize,)),
      ],
    );
  }
}
